home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
500 MB Nyheder Direkte fra Internet 2
/
500 MB nyheder direkte fra internet CD 2.iso
/
start
/
data
/
text
/
calcs.txt
< prev
next >
Wrap
Text File
|
1994-09-21
|
48KB
|
1,148 lines
EXPONENTS
The computer understands exponents:
PRINT 4^3
That line makes the computer use the number 4, three times. The
computer will multiply together those three 4's, like this: 4
times 4 times 4. Since ``4 times 4 times 4'' is 64, the computer
will print:
64
The symbols +, -, *, /, and ^ are all called operations.
To solve a problem, the computer uses the three-step process
taught in algebra and the ``new math''. For example, suppose you
ask the computer to figure out 70-3^2+8/2*3. The computer will
not begin by subtracting 3 from 70; instead, it will use the
three-step process:
The problem is70-3^2+8/2*3
Step 1: get rid of ^.Now the problem is70- 9 +8/2*3
Step 2: get rid of * and /.Now the problem is70- 9 + 12
Step 3: get rid of + and -.The answer is 73
In each step, it looks from left to right. For example, in step
2, it sees / and gets rid of it before it sees *.
Although exponents are fun, the computer handles them slowly.
For example, the computer handles 3^2 more slowly than it handles
3*3. So for speedy calculations, say 3*3 instead of 3^2.
Roots
What positive number, when multiplied by itself, gives 9? The
answer is 3, because 3 times itself is 9.
3 squared is 9. 3 is called the square root of 9.
To make the computer deduce the square root of 9, type this:
PRINT SQR(9)
The computer will print 3.
When you tell the computer to PRINT SQR(9), make sure you put
the parentheses around the 9.
The symbol SQR is called a function. The number in parentheses
(9) is called the function's input (or argument or parameter).
The answer, which is 3, is called the function's output (or
value).
SQR(9) gives the same answer as 9^.5. Most computers handle
SQR(9) more quickly than 9^.5.
Cube roots What number, when multiplied by itself and then
multiplied by itself again, gives 64? The answer is 4, because 4
times 4 times 4 is 64. The answer (4) is called the cube root of
64.
Here's how to make the computer find the cube root of 64:
PRINT 64^(1/3)
The computer will print 4.
EXP
The letter ``e'' stands
for a special number, which is approximately 2.718281828459045.
You can memorize that number easily, if you pair the digits:
2.7 18 28 18 28 45 90 45
That weird number is
important in calculus, radioactivity, biological growth, and
other areas of science. It's calculated by this formula:
1 1 1 1 1
e = 1 + + + + + . . .
1 1*2 1*2*3 1*2*3*4 1*2*3*4*5
Therefore:
1 1 1 1
e = 1 + 1 + + + + + . . .
2 6 24 120
EXP(X) means eX. For
example, EXP(3) means e3, which is e*e*e, which is:
2.718281828459045*2.718281828459045*2.718281828459045
EXP(4) means e4, which is e*e*e*e. EXP(3.1) means e3.1, which is
more than e3 but less than e4.
Here's a practical
application. Suppose you put $732 in a savings account, and the
bank promises to give you 5% annual interest ``compounded
continuously''. How much money will you have at the end of the
year? The answer is 732*EXP(.05).
Logarithms
Here are some powers of 2:
X 2X
1 2
2 4
3 8
4 16
5 32
6 64
To compute the logarithm-base-2 of a number, find the number in
the right-hand column; the answer is in the left column. For
example, the logarithm-base-2 of 32 is 5. The logarithm-base-2 of
15 is slightly less than 4.
The logarithm-base-2 of 64 is 6. That fact is written:
log2 64 is 6
It's also written:
log 64
is 6
log 2
To make the computer find the logarithm-base-2 of 64, say:
PRINT LOG(64)/LOG(2)
The computer will print 6.
Here are some powers of 10:
X 10X
1 10
2 100
3 1000
4 10000
5 100000
The logarithm-base-10 of 100000 is 5. The logarithm-base-10 of
1001 is slightly more than 3.
The logarithm-base-10 of 10000 is 4. That fact is written:
log10 10000 is 4
It's also written:
log 10000
is 4
log 10
To make the computer do that calculation, say:
PRINT LOG(10000)/LOG(10)
The computer will print 4.
The logarithm-base-10 is called the common logarithm, and is
the kind of logarithm used in high school and chemistry. So if
you're studying chemistry and your textbook tells you to find the
logarithm of 10000, the textbook means the logarithm-base-10 of
10000, which is LOG(10000)/LOG(10).
What happens if you forget the base, and say just LOG(10000)
instead of LOG(10000)/LOG(10)? If you say just LOG(10000), the
computer will find the natural logarithm of 10000, which is loge
10000 (where e is 2.718281828459045), which isn't what your
chemistry textbook wants.
CONTRASTS
The computer's notation
resembles that of arithmetic and algebra, but beware of these
contrasts. . . .
Multiplication
To make the computer
multiply, you must type an asterisk:
Traditional notation Computer
notation
2n 2*N
5(n+m) 5*(N+M)
nm N*M
Exponents
Put an exponent in
parentheses, if it contains an operation:
Traditional notation Computer
notation
xn+2 X^(N+2)
x3n X^(3*N)
52/3 5^(2/3)
234 2^(3^4)
Fractions
Put a fraction's
numerator in parentheses, if it contains addition or subtraction:
Traditional notation Computer
notation
a+b
(A+B)/C
c
k-20
(K-20)/6
6
Put a denominator in
parentheses, if it contains addition, subtraction,
multiplication, or division:
Traditional notation Computer
notation
5
5/(3+X)
3+x
5a3
5*A^3/(4*B)
4b
Mixed numbers
A mixed number is a
number that contains a fraction. For example, 9½ is a mixed
number. When you write a mixed number, put a plus sign before its
fraction:
Traditional notation Computer
notation
9½ 9+1/2
If you're using the
mixed number in a further calculation, put the mixed number in
parentheses:
Traditional notation Computer
notation
7-2¼ 7-(2+1/4)
STRIPPING
Sometimes the computer prints too much information: you wish
the computer would print less, to save yourself the agony of
having to read excess information that's irrelevant to your
needs. Whenever the computer prints too much information about a
numerical answer, use ABS, INT, or SGN.
ABS removes any minus sign. For example, the ABS of -7.926 is
7.926. So if you say PRINT ABS(-7.926), the computer will print
just 7.926.
INT removes any digits after the decimal point, and rounds the
number down to an integer that's lower. For example, the INT of
7.926 is 7 (because 7 is an integer that's lower than 7.926);
the INT of -7.926 is -8 (because -8 is lower than -7.926).
SGN removes all the digits and replaces them by a 1 ___ unless
the number is 0. For example, the SGN of 7.926 is 1. The SGN of
-7.926 is -1. The SGN of 0 is just 0.
ABS, INT, and SGN are all called stripping functions or
strippers or diet functions or diet pills, because they strip
away the number's excess fat and reveal just the fundamentals
that interest you.
Here are more details about those three functions. . . .
ABS
To find the absolute value of a negative number, just omit the
number's minus sign. For example, the absolute value of -7 is 7.
The absolute value of a positive number is the number itself.
For example, the absolute value of 7 is 7.
To make the computer find the absolute value of -7, type this:
PRINT ABS(-7)
The computer will print:
7
Like SQR, ABS is a function: you must put parentheses after the
ABS.
ABS helps you solve math and physics problems that involve
``distance''. For example, this program computes the distance
between two numbers:
10 PRINT "I WILL FIND THE DISTANCE BETWEEN TWO NUMBERS."
20 INPUT "WHAT'S THE FIRST NUMBER";X
30 INPUT "WHAT'S THE SECOND NUMBER";Y
40 PRINT "THE DISTANCE BETWEEN THOSE NUMBERS IS";ABS(X-Y)
For example, if X is 4, and Y is 7, then the distance between
those two numbers is ABS(4-7), which is ABS(-3), which is 3. If
you reverse those two numbers, so that X is 7 and Y is 4, the
distance between them is ABS(7-4), which is ABS(3), which is
still 3.
INT
If you round 17.9 to an
integer, what do you get?
If you round 17.9 to the
nearest integer, you get 18. If you round 17.9 down to an
integer, you get 17. If you round 17.9 up to an integer, you get
18.
To make the computer
round 17.9 down to an integer, type this:
PRINT INT(17.9)
The computer will print:
17
Notice that INT rounds
down. INT(17.9) tells the computer to round 17.9 down to an
integer; the computer gets 17.
Like SQR and ABS, INT is
a function: you must put parentheses after the INT.
If you give this command
___
PRINT INT(-5.2)
what number will the computer print? Will it print -5? Or will it
print -6 instead? Answer: the computer will print -6, because if
today's temperature is -5.2 degrees, and you round the
temperature down, the temperature becomes colder: -6 degrees. INT
rounds down. INT(-5.2) is -6.
INT(54) is simply 54.
To explore further the
mysteries of rounding, run this program:
10 INPUT "WHAT'S YOUR FAVORITE NUMBER";X
20 PRINT INT(X)
30 PRINT -INT(-X)
40 PRINT INT(X+.5)
In that program, line 10 asks you to type a number X. Line 20
prints your number rounded down; line 30 prints your number
rounded up; and line 40 prints your number rounded to the nearest
integer. For example, if you input 17.9, line 20 makes the
computer print 17.9 rounded down (which is 17), line 30 makes the
computer print 17.9 rounded up (which is 18), and line 40 makes
the computer print 17.9 rounded to the nearest integer (which is
18).
Here's the rule: if X is
a number, INT(X) rounds X down; -INT(-X) rounds X up; INT(X+.5)
rounds X to the nearest integer.
Rounding down and
rounding up are useful in the supermarket. . . .
Suppose some items are
marked ``30¢ each'', and you have only two dollars. How many can
you buy? Two dollars divided by 30¢ is 6.66667; rounding down to
an integer, you can buy 6.
Suppose some items are
marked ``3 for a dollar'', and you want to buy just one of them.
How much will the supermarket charge you? One dollar divided by 3
is 33.3333¢; rounding up to an integer, you will be charged 34¢.
By using INT, you can do fancier kinds of rounding:
to round X to the nearest thousand, ask for INT(X/1000+.5)*1000
to round X to the nearest thousandth, ask for INT(X/.001+.5)*.001
This program rounds a number, so that it will have just a few
digits after the decimal point:
10 INPUT "WHAT'S YOUR FAVORITE NUMBER";X
20 INPUT "HOW MANY DIGITS WOULD YOU LIKE AFTER ITS DECIMAL
POINT";D
30 B=10^-D
40 PRINT "YOUR NUMBER ROUNDED IS";INT(X/B+.5)*B
Here's a sample run:
WHAT'S YOUR FAVORITE NUMBER? 4.28631
HOW MANY DIGITS WOULD YOU LIKE AFTER ITS DECIMAL POINT? 2
YOUR NUMBER ROUNDED IS 4.29
SGN
If a number is negative, its sign is -1. For example, the sign
of -546 is -1.
If a number is positive, its sign is +1. For example the sign
of 8231 is +1.
The sign of 0 is 0.
The computer's abbreviation for ``sign'' is ``SGN''. So if you
say ___
PRINT SGN(-546)
the computer will print the sign of -546; it will print -1.
If you say ___
PRINT SGN(8231)
the computer will print the sign of 8231; it will print 1.
If you say ___
PRINT SGN(0)
the computer will print the sign of 0; it will print 0.
SGN is the opposite of ABS. Let's see what both functions do to
-7.2. ABS removes the minus sign, but leaves the digits:
ABS(-7.2) is 7.2
SGN removes the digits, but leaves the minus sign:
SGN(-7.2) is -1
The Latin word for sign is signum. Most mathematicians prefer
to talk in Latin ___ they say ``signum'' instead of ``sign'' ___
because the English word ``sign'' sounds too much like the
trigonometry word ``sine''. So mathematicians call SGN the signum
function.
RANDOM NUMBERS
Usually, the computer is predictable: it does exactly what you
say. But sometimes, you want the computer to be unpredictable.
For example, if you're going to play a game of cards with the
computer and tell the computer to deal, you want the cards dealt
to be unpredictable. If the cards were predictable ___ if you
could figure out exactly which cards you and the computer would
be dealt ___ the game would be boring.
In many other games too, you want the computer to be
unpredictable, to ``surprise'' you. Without an element of
surprise, the game would be boring.
Being unpredictable increases the pleasure you derive from
games ___ and from art. To make the computer act artistic, and
create a new original masterpiece that's a ``work of art'', you
need a way to make the computer get a ``flash of inspiration''.
Flashes of inspiration aren't predictable: they're surprises.
Here's how to make the computer act unpredictably. . . .
Random integers
This program makes the computer print an unpredictable number
from 1 to 5:
10 RANDOMIZE
20 PRINT RND(5)
Unpredictable numbers are called random numbers. Line 10 tells
the computer to make the numbers be completely unpredictable,
completely random. Line 20 makes the computer print a random
number from 1 to 5, so the computer will print. The computer's
choice will be a surprise.
For example, when you type RUN, the computer might print 3. If
you run the program a second time, the computer might print a
different number (1 or 2 or 4 or 5), or it might print the same
number (3). You can't predict which number the computer will
print. The only thing you can be sure of is: the number will be
from 1 to 5.
For YOUR computer, you'll probably have to write the program
slightly differently. To find out your computer's peculiarities,
check the ``Versions of BASIC'' appendix. For example, if you
have an IBM PC or clone, that appendix tells you to say
``RANDOMIZE TIMER'' instead of just ``RANDOMIZE'', and to say
1+INT(RND*5) instead of just RND(5).
To make the computer print many such random numbers, say GO TO:
10 RANDOMIZE
20 PRINT RND(5)
30 GO TO 20
The computer will print many numbers, like this:
3
2
4
4
1
3
5
2
2
5
etc.
Each number will be 1 or 2 or 3 or 4 or 5. The order in which the
computer prints them is unpredictable. The program's an infinite
loop: it won't stop until you abort it. If you run the
program again, the pattern will be different; for example, it
might be:
1
4
3
3
2
5
1
1
2
etc.
When you run that
program, the numbers will fly up the screen faster than you can
read. To make the numbers easier to read, make the computer print
them across instead of down; make the computer print like this:
3 2 4 4 1 3 5 2 2 5 etc.
To do that, put a semicolon in the PRINT statement:
10 RANDOMIZE
20 PRINT RND(5);
30 GO TO 20
That program prints
random numbers up to 5. To see random numbers up to 1000, say
RND(1000):
10 RANDOMIZE
20 PRINT RND(1000);
30 GO TO 20
The computer will print something like this:
485 729 8 537 1000 13 1 842 842 156 1000 972 etc.
Guessing game
This program plays a
guessing game:
10 RANDOMIZE
20 PRINT "I'M THINKING OF A NUMBER FROM 1 TO 100."
30 C=RND(100)
40 INPUT "WHAT DO YOU THINK MY NUMBER IS";G
50 IF G<C THEN PRINT "YOUR GUESS IS TOO LOW.": GO TO 40
60 IF G>C THEN PRINT "YOUR GUESS IS TOO HIGH.": GO TO 40
70 PRINT "CONGRATULATIONS! YOU FOUND MY NUMBER!"
Line 20 makes the
computer say:
I'M THINKING OF A NUMBER FROM 1 TO 100.
Line 30 makes the computer think of a random number from 1 to
100; the computer's number is called ``C''. Line 40 asks the
human to guess the number; the guess is called ``G''.
If the guess is less
than the computer's number, line 50 makes the computer say ``YOUR
GUESS IS TOO LOW'' and then GO TO 40, which lets the human guess
again. If the guess is greater than the computer's number, line
60 makes the computer say ``YOUR GUESS IS TOO HIGH'' and then GO
TO 40.
When the human guesses
correctly, the computer arrives at line 70, which prints:
CONGRATULATIONS! YOU FOUND MY NUMBER!
Here's a sample run:
RUN
I'M THINKING OF A NUMBER FROM 1 TO 100.
WHAT DO YOU THINK MY NUMBER IS? 54
YOUR GUESS IS TOO LOW.
WHAT DO YOU THINK MY NUMBER IS? 73
YOUR GUESS IS TOO HIGH.
WHAT DO YOU THINK MY NUMBER IS? 62
YOUR GUESS IS TOO LOW.
WHAT DO YOU THINK MY NUMBER IS? 68
YOUR GUESS IS TOO LOW.
WHAT TO YOU THINK MY NUMBER IS? 70
YOUR GUESS IS TOO HIGH.
WHAT DO YOU THINK MY NUMBER IS? 69
CONGRATULATIONS! YOU FOUND MY NUMBER!
Dice
This program makes the computer roll a pair of dice:
10 RANDOMIZE
20 PRINT "I'M ROLLING A PAIR OF DICE"
30 A=RND(6)
40 PRINT "ONE OF THE DICE SAYS";A
50 B=RND(6)
60 PRINT "THE OTHER SAYS";B
70 PRINT "THE TOTAL IS";A+B
Line 20 makes the computer say:
I'M ROLLING A PAIR OF DICE
Each of the dice has 6 sides. Lines 30 and 40 roll one of the
dice, by picking a number from 1 to 6. Lines 50 and 60 roll the
other. Line 70 prints the total.
Here's a sample run:
I'M ROLLING A PAIR OF DICE
ONE OF THE DICE SAYS 3
THE OTHER SAYS 5
THE TOTAL IS 8
Here's another run:
I'M ROLLING A PAIR OF DICE
ONE OF THE DICE SAYS 6
THE OTHER SAYS 4
THE TOTAL IS 10
Coin flipping
This program makes the computer flip
a coin:
10 RANDOMIZE
20 IF RND(2)=1 THEN PRINT "HEADS" ELSE PRINT "TAILS"
RND(2) is a random number from 1 to
2; so it's either 1 or 2. If it's 1, line 20 makes the computer
says HEADS; if it's 2 instead, line 20 makes the computer say
TAILS.
Until you type RUN, you won't know
which way the coin will flip; the choice is random. Each time you
type RUN, the computer will flip the coin again; each time, the
outcome is unpredictable.
(Warning: if your computer's too
stupid to understand the word ELSE, you must retype line 20; the
``Versions of BASIC'' appendix explains how.)
Bets Let's permit the human to bet
on whether the computer will say HEADS or TAILS. Here's how:
10 RANDOMIZE
15 INPUT "DO YOU WANT TO BET ON HEADS OR TAILS";B$
16 IF B$<>"HEADS" AND B$<>"TAILS" THEN PRINT "SAY HEADS OR
TAILS": GO TO 15
20 IF RND(2)=1 THEN C$="HEADS" ELSE C$="TAILS"
30 PRINT "THE COIN SAYS ";C$
40 IF C$=B$ THEN PRINT "YOU WIN" ELSE PRINT "YOU LOSE"
Line 15 makes the computer ask:
DO YOU WANT TO BET ON HEADS OR TAILS?
Line 16 makes sure the human says HEADS or TAILS: if the human's
answer isn't HEADS and isn't TAILS, the computer gripes. Lines 20
and 30 make the computer flip a coin. Line 40 determines whether
the human won or lost the bet.
Here's a sample run:
DO YOU WANT TO BET ON HEADS OR TAILS? HEADS
THE COIN SAYS TAILS
YOU LOSE
Here's another:
DO YOU WANT TO BET ON HEADS OR TAILS? TAILS
THE COIN SAYS TAILS
YOU WIN
Here's another:
DO YOU WANT TO BET ON HEADS OR TAILS? TAILS
THE COIN SAYS HEADS
YOU LOSE
Money To make the program more fun, let the human use money
when betting:
1 RANDOMIZE
2 S=100
3 PRINT "YOU HAVE";S;"DOLLARS"
10 INPUT "HOW MANY DOLLARS DO YOU WANT TO BET";B
11 IF B>S THEN PRINT "YOU DON'T HAVE THAT MUCH! YOU MUST BET
LESS!": GO TO 10
12 IF B<0 THEN PRINT "YOU CAN'T BET LESS THAN NOTHING!": GO TO 10
13 IF B=0 THEN PRINT "I GUESS YOU DON'T WANT TO BET ANYMORE": GO
TO 100
15 INPUT "DO YOU WANT TO BET ON HEADS OR TAILS";B$
16 IF B$<>"HEADS" AND B$<>"TAILS" THEN PRINT "SAY HEADS OR
TAILS": GO TO 15
20 IF RND(2)=1 THEN C$="HEADS" ELSE C$="TAILS"
30 PRINT "THE COIN SAYS ";C$
40 IF C$=B$ THEN PRINT "YOU WIN";B;"DOLLARS": S=S+B: GO TO 3
50 PRINT "YOU LOSE";B;"DOLLARS": S=S-B: IF S>0 THEN GO TO 3
60 PRINT "YOU'RE BROKE! TOO BAD!"
100 PRINT "THANKS FOR PLAYING WITH ME! YOU WERE FUN TO PLAY
WITH!"
110 PRINT "I HOPE YOU PLAY AGAIN SOMETIME"
Lines 2 and 3 make the computer say:
YOU HAVE 100 DOLLARS
Line 10 makes the computer ask:
HOW MANY DOLLARS DO YOU WANT TO BET?
Lines 11-13 make sure the bet is reasonable. Lines 15 and 16 get
the human to bet on heads or tails. Lines 20 and 30 flip the
coin. Lines 40 and 50 determine whether the human won or lost the
bet, and then send the computer back to line 3 for another round
(if the human isn't broke yet). Lines 60-110 say good-bye to the
human.
Here's a sample run:
YOU HAVE 100 DOLLARS
HOW MANY DOLLARS DO YOU WANT TO BET? 120
YOU DON'T HAVE THAT MUCH! YOU MUST BET LESS!
HOW MANY DOLLARS DO YOU WANT TO BET? 75
DO YOU WANT TO BET ON HEADS OR TAILS? HEADS
THE COIN SAYS TAILS
YOU LOSE 75 DOLLARS
YOU HAVE 25 DOLLARS
HOW MANY DOLLARS DO YOU WANT TO BET? 10
DO YOU WANT TO BET ON HEADS OR TAILS? TAILS
THE COIN SAYS TAILS
YOU WIN 10 DOLLARS
YOU HAVE 35 DOLLARS
HOW MANY DOLLARS DO YOU WANT TO BET? 35
DO YOU WANT TO BET ON HEADS OR TAILS? TAILS
THE COIN SAYS HEADS
YOU LOSE 35 DOLLARS
YOU'RE BROKE! TOO BAD!
THANKS FOR PLAYING WITH ME! YOU WERE FUN TO PLAY WITH!
I HOPE YOU PLAY AGAIN SOMETIME
Displaying all the dollars To make the output prettier, insert
these lines:
3 PRINT
4 PRINT "YOU HAVE";S;"DOLLARS! HERE THEY ARE:"
5 FOR I = 1 TO S
6 PRINT "$";
7 NEXT
8 PRINT
Now the run looks like this:
RUN
$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
$$$$$$$$$$$$$$$
$$$$$$$$$$$$$$$$$$$$
HOW MANY DOLLARS DO YOU WANT TO BET? 120
YOU DON'T HAVE THAT MUCH! YOU MUST BET LESS!
HOW MANY DOLLARS DO YOU WANT TO BET? 75
DO YOU WANT TO BET ON HEADS OR TAILS? HEADS
THE COIN SAYS TAILS
YOU LOST 75 DOLLARS
YOU HAVE 25 DOLLARS! HERE THEY ARE:
$$$$$$$$$$$$$$$$$$$$$$$$$
HOW MANY DOLLARS DO YOU WANT TO BET? 10
DO YOU WANT TO BET ON HEADS OR TAILS? TAILS
THE COIN SAYS TAILS
YOU WIN 10 DOLLARS
YOU HAVE 35 DOLLARS! HERE THEY ARE:
$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
HOW MANY DOLLARS DO YOU WANT TO BET? 35
DO YOU WANT TO BET ON HEADS OR TAILS? TAILS
THE COIN SAYS HEADS
YOU LOSE 35 DOLLARS
YOU'RE BROKE! TOO BAD!
THANKS FOR PLAYING WITH ME! YOU WERE FUN TO PLAY WITH!
I HOPE YOU PLAY AGAIN SOMETIME
Your friends
This program makes the computer reveal the secret desires of
your friends:
10 RANDOMIZE
20 INPUT "TYPE THE NAME OF SOMEONE YOU LIKE...";N$
30 IF RND(3)=1 THEN PRINT N$;" WANTS TO TICKLE YOUR TOES" ELSE
PRINT N$;" WANTS
YOU TO WIGGLE YOUR NOSE"
40 GO TO 20
Line 20 makes the computer ask:
TYPE THE NAME OF SOMEONE YOU LIKE...?
Suppose you say SUE. Then N$ is SUE. Line 30 makes the computer
pick a random number up to 3. If the number is 1, the computer
will say SUE WANTS TO TICKLE YOUR TOES; but if the number is 2 or
3 instead, the computer will say SUE WANTS YOU TO WIGGLE YOUR
NOSE. Line 40 makes the computer go back to line 20 and analyze
your other friends also.
In that program, the chance is only 1 out of 3 that the
computer will say TICKLE YOUR TOES. The chance is 2 out of 3 that
the computer will say WIGGLE YOUR NOSE instead.
Get together with your friends and run that program. For
exciting results, take off your shoes, and put lipstick on your
nose.
Daily horoscope
This program predicts what will happen to you today:
10 RANDOMIZE
20 PRINT "YOU WILL HAVE A ";
30 R=RND(5)
40 IF R=1 THEN PRINT "WONDERFUL";
50 IF R=2 THEN PRINT "BETTER-THAN-AVERAGE";
60 IF R=3 THEN PRINT "SO-SO";
70 IF R=4 THEN PRINT "WORSE-THAN-AVERAGE";
80 IF R=5 THEN PRINT "TERRIBLE";
90 PRINT " DAY TODAY"
The computer will say ___
YOU WILL HAVE A WONDERFUL DAY TODAY
or ___
YOU WILL HAVE A TERRIBLE DAY TODAY
or some in-between comment.
For inspiration, run that program when you get up in the
morning.
Random decimals
You've seen
that RND(5) is a random number from 1 to 5: it's 1 or 2 or 3 or 4
or 5. To get a random decimal between 0 and 1, say just RND
instead of RND(5).
The decimal
that RND produces is at least 0 and is less than 1, so it can be
any decimal from 0.0000000 to 0.9999999. For example, the decimal
might be 0.2845918.
Suppose you
want the computer to maybe print LOVE. Here's how to make the
probability of printing LOVE be 37 percent:
10 RANDOMIZE
20 IF RND<.37 THEN PRINT "LOVE"
CHARACTER CODES
Each character has a code number. For example, the code number
for ``A'' is 65; the code number for ``B'' is 66; the code number
for ``C'' is 67; etc.
Those code numbers form the American Standard Code for
Information Interchange, which is abbreviated ASCII, which is
pronounced ``ass key''. Programmers say, ``the ASCII code number
for A is 65''.
If you say ___
PRINT ASC("A")
the computer will print the ASCII code number for ``A''. It will
print:
65
Similarly, if you say PRINT ASC(``B''), the computer will print
66.
If you say ___
PRINT CHR$(65)
the computer will print the CHaRacter whose code number is 65. It
will print:
A
The code number for a capital ``A'' is 65, capital ``B'' is 66,
capital ``C'' is 67, etc. The code number for a small ``a'' is
97, small ``b'' is 98, small ``c'' is 99, etc. The code number
for the digit ``0'' is 48, the digit ``1'' is 49, the digit ``2''
is 50, etc.
Here's the complete list of code numbers, from 33 to 126:
Character: ! " # $ % & ' ( ) * + , - . / 0 1... 9
: ; < = >
Code #:33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49...57 58
59 60 61 62
Character: ? @ A B... Z [ \ ] ^ _ ` a b... z { |
} ~
Code #:63 64 65 66...90 91 92 93 94 95 96 97 98...122 123 124 125
126
32 is the code number for a blank space. 127 is the code number
for DELETE.
Notice that the code number for a quotation mark is 34. Here's
how to make the computer print a quotation mark:
PRINT CHR$(34)
Suppose you want the computer to print:
SCHOLARS THINK "HAMLET" IS A GREAT PLAY
To make the computer print the quotation marks around ``HAMLET'',
use CHR$(34), like this:
PRINT "SCHOLARS THINK ";CHR$(34);"HAMLET";CHR$(34);" IS A GREAT
PLAY"
Control codes
On the typical computer's keyboard, you'll see a key marked
CONTROL (or CTRL). That CONTROL key is near the left SHIFT key.
While you hold down the CONTROL key, you can tap the A key;
that's called a controlled A. The code number for a controlled A
is 1. Similarly, the code number for a controlled B is 2; a
controlled C is 3; etc.
Holding down the CONTROL key subtracts 64 from the code number
of the other key. For example, the A key is normally 65, so a
controlled A is 65-64, which is 1. The B key is normally 66, so a
controlled B is 66-64, which is 2.
You've seen that a controlled A has code 1, and a controlled B
has code 2. Controlled @ has code 0. In fact, by using the
CONTROL key, you can generate all the characters that have codes
from 0 to 31.
Graphics codes
The code numbers from 128 up to 255 are used for graphics.
Explore
To explore
your computer's ability to print both text and graphics, run this
program:
10 FOR I = 33 TO 255
20 PRINT I;CHR$(I);" ";
30 NEXT
The computer will print each number and its associated character,
like this:
33 ! 34 " 35 # 36 $ etc.
When the computer finally reaches 128, it will start printing
graphics characters.
In that
program, you can try starting I at a number lower than 33. For
example, you can try starting I at 5. But be careful: some of
those low numbers will make your computer act very strangely!
On the
Apple, do not say PRINT CHR$(4). The CHR$(4) might make your
computer turn on the disk drive and start wrecking your disk!
If your
computer is typical, you can make it beep by saying:
PRINT CHR$(7)
Printer codes
If you say ___
PRINT "LOVE"
the computer will print LOVE on your screen. If you say ___
LPRINT "LOVE"
the typical computer will print LOVE on paper (instead of your
screen) by using a printer.
The most popular printers are manufactured by Epson. You can
make an Epson printer do weird things on paper, by giving a
special CHR$ command.
An Epson normally prints 10 characters per inch. You can change
that:
Purpose Command
print 17 characters per inch, so the characters are
condensedLPRINT CHR$(15);
cancel the condensed printing LPRINT CHR$(18);
print 12 characters per inch, so the characters are eliteLPRINT
CHR$(27)"M";
cancel the elite printing LPRINT CHR$(27)"P";
print 5 characters per inch, so the characters are enlargedLPRINT
CHR$(14);
cancel the enlarged printing LPRINT CHR$(20);
print with proportional spacing, so ``m'' is wider than
``i''LPRINT CHR$(27)"p1";
cancel the proportional spacingLPRINT CHR$(27)"p0";
When an Epson finishes printing a line of characters, it
normally jerks the paper up a sixth of an inch (so the next line
of characters is a sixth of an inch below the first line, and the
Epson prints six lines per inch). Computerists say, ``The Epson
normally feeds a sixth of an inch.'' You can change that amount:
Purpose Command
feed 1/8 of an inch LPRINT CHR$(27)"0";
feed about 1/10 of an inch (7/72 inch)LPRINT CHR$(27)"1";
feed n/72 of an inchLPRINT CHR$(27)"A"CHR$(n);
feed n/216 of an inchLPRINT CHR$(27)"3"CHR$(n);
go back to normal: feed 1/6 of an inchLPRINT CHR$(27)"2";
Here are some other fancy commands you can give:
Purpose Command
beep for about a tenth of a secondLPRINT CHR$(7);
print italics LPRINT CHR$(27)"4";
cancel the italics LPRINT CHR$(27)"5";
print subscripts (tiny char. below line)LPRINT CHR$(27)"S1";
print superscripts (tiny char. above line)LPRINT CHR$(27)"S0";
cancel subscripts and superscriptsLPRINT CHR$(27)"T";
horizontal fill, so that `` '' becomes `` ''LPRINT CHR$(27)"E";
cancel the horizontal fillLPRINT CHR$(27)"F";
vertical fill, so that `` '' becomes `` ''LPRINT CHR$(27)"G";
cancel the vertical fillLPRINT CHR$(27)"H";
move to the top of the next pageLPRINT CHR$(12);
move to the right, to the next tab stopLPRINT CHR$(9);
move left, to the previous characterLPRINT CHR$(9);
move left, all the way to the marginLPRINT CHR$(27)"<";
make left margin be n characters wideLPRINT CHR$(27)"l"CHR$(n);
make right margin be at position nLPRINT CHR$(27)"Q"CHR$(n);
cancel all previous CHR$ commandsLPRINT CHR$(27)"Q";
STRING ANALYSIS
Let's analyze the word ``SMART''.
Length
Since ``SMART'' has 5 characters in it, the length of ``SMART''
is 5. If you say ___
PRINT LEN("SMART")
the computer will print the LENgth of ``SMART''; it will print:
5
Left, right, middle
The left two characters of ``SMART'' are ``SM''. If you say ___
PRINT LEFT$("SMART",2)
the computer will print:
SM
Try this program:
10 A$="SMART"
20 PRINT LEFT$(A$,2)
Line 10 says A$ is ``SMART''. Line 20 says to print the left 2
characters of A$, which are ``SM''. The computer will print:
SM
If A$ is ``SMART'', here are the consequences. . . .
LEN(A$) is the LENgth of A$. It is 5.
LEFT$(A$,2) is the LEFT 2 characters of A$. It is ``SM''.
RIGHT$(A$,2) is the RIGHT 2 characters of A$. It is ``RT''.
MID$(A$,2) is the MIDdle of A$, beginning at the 2nd character.
It is ``MART''.
MID$(A$,2,3) begins at the 2nd character and includes 3
characters. It's ``MAR''.
Changing the middle You can change the middle of a string, like
this:
10 A$="BUNKERS"
20 MID$(A$,2)="OWL"
30 PRINT A$
Line 10 says A$ is ``BUNKERS''. Line 20 changes the middle of A$
to ``OWL''; the change begins at the 2nd character of A$. Line 30
prints:
BOWLERS
Here's a variation:
10 A$="BUNKERS"
20 MID$(A$,2)="AD AGENCY"
30 PRINT A$
Line 10 says A$ is ``BUNKERS''. Line 20 says to change the middle
of A$, beginning at the 2nd character of A$. But ``AD AGENCY'' is
too long to become part of ``BUNKERS''. The computer uses as much
of ``AD AGENCY'' as will fit in ``BUNKERS''. The computer will
print:
BAD AGE
Another variation:
10 A$="BUNKERS"
20 MID$(A$,2,1)="OWL"
30 PRINT A$
Line 10 says A$ is ``BUNKERS''. Line 20 says to change the middle
of A$, beginning at the 2nd character of A$. But the ``,1'' makes
the computer use just 1 letter from ``OWL''. Line 30 prints:
BONKERS
Adding strings
You can add strings together, to form a longer string:
10 A$="FAT"+"HER"
20 PRINT A$
Line 10 says A$ is ``FATHER''. Line 20 makes the computer print:
FATHER
Searching in a string
You can make the computer search in a string to find another
string. To make the computer search IN the STRing ``NEEDED'' to
find ``ED'', say:
PRINT INSTR("NEEDED","ED")
Since ``ED'' begins at the third character of ``NEEDED'', the
computer will print:
3
If you say ___
PRINT INSTR("NEEDED","EY")
the computer will search in the string ``NEEDED'' for ``EY''.
Since ``EY'' is not in ``NEEDED'', the computer will print:
0
If you say ___
PRINT INSTR(4,"NEEDED","ED")
the computer will hunt in the string ``NEEDED'' for ``ED''; but
the hunt will begin at the 4th character of ``NEEDED''. The
computer finds the ``ED'' that begins at the 5th character of
``NEEDED''. The computer will print:
5
Clock
The typical computer has a built-in clock. To set the clock to
7 seconds after 1:45PM, type this:
TIME$="13:45:07"
To set the date to January 24, 1996, type this:
DATE$="01-24-1996"
Afterwards, whenever you want to find out the current time and
date, type this:
PRINT TIME$
PRINT DATE$
If you say ___
PRINT TIMER
the computer will tell you how many seconds have elapsed since
midnight.
When you turn off the typical computer, it forgets the time and
date. When you turn it on again, tell it the new time and date.
String-number conversion
This program converts a
string to a number:
10 A$="72.6"
20 B=VAL(A$)
30 PRINT B+1
Line 10 says A$ is the
string ``72.6''. Line 20 says B is the numeric VALue of A$, so B
is the number 72.6. Line 30 prints:
73.6
VAL converts a string to
a number. The opposite of VAL is STR$, which converts a number to
a string. For example, STR$(-7.2) is the string ``-7.2''.
STR$(81.4) is the string
`` 81.4''. Notice that in the string `` 81.4'', the 8 comes after
a space (instead of coming after a minus sign).
Repeating characters
Suppose you love the
letter B (because it stands for Big, Bold, and Beautiful) and
want to print ``BBBBBBBBBBBBBBBBBBBB''. Here's a short-cut:
PRINT STRING$(20,"B")
That tells the computer to print a string of 20 B's.
Here's a different way
to accomplish the same goal:
PRINT STRING$(20,66)
That tells the computer to print, 20 times, the character whose
ASCII code number is 66.
STRING$ can make the
computer repeat a single character, but not a whole word. So if
you say STRING$(20,``BLOW''), the computer will not repeat the
word ``BLOW''; instead, the computer will repeat just the first
character of ``BLOW'' (which is ``B'').
TRIGONOMETRY
The study of triangles is called trigonometry ___ and the
computer can do it for you!
For example, look at this triangle:
In that triangle, the left angle is 30°, the lower-right angle is
90°, and the longest side (the hypotenuse) is 1 inch long.
The side opposite the 30° angle is called the sine of 30°; the
remaining side is called the cosine of 30°:
How long is the sine of 30°? How long is the cosine of 30°?
Since the longest side (the hypotenuse) is 1 inch long, and
since the sine and the cosine are shorter sides, the sine and the
cosine must each be shorter than 1 inch. So the lengths of the
sine and cosine are each less than 1. But which decimals are
they?
To find out, you can use a ruler. You'll discover that the sine
is half an inch long, and the cosine is nearly seven-eighths of
an inch long. But a faster and more accurate way to measure the
sine and cosine is to let the computer do it! Yes, the computer
can calculate triangles in its mind!
This program makes the computer measure the sine and cosine of
30°:
10 D=ATN(1)/45
20 PRINT SIN(30*D)
30 PRINT COS(30*D)
Line 10 is a special formula that defines D to mean ``degrees''.
Line 20 prints the sine of 30 degrees; the computer will print:
.5
Line 30 prints the cosine of 30 degrees; the computer will print
a decimal that's slightly less than .87.
The computer can measure the sine and cosine of any size angle.
Try it! For example, to make the computer print the sine and
cosine of a 33° angle, say:
10 D=ATN(1)/45
20 PRINT SIN(33*D)
30 PRINT COS(33*D)
If you choose an angle of -33° instead of 33°, the triangle
will dip down instead of rising up, and so the sine will be a
negative number instead of positive.
In lines 20 and 30, the ``*D'' is important: it tells the
computer that you want the sine of 33 degrees. If you
accidentally omit the ``*D'', the computer will print the sine of
33 radians instead. (A radian is larger than a degree. A radian
is about 57.3 degrees. More precisely, a radian is 180/π
degrees.)
Tangent
The sine divided by the
cosine is called the tangent. For example, to find the tangent of
33°, divide the sine of 33° by the cosine of 33°.
To make the computer
print the tangent of 33°, you could tell the computer to PRINT
SIN(33*D)/COS(33*D). But to find the tangent more quickly and
easily, just say PRINT TAN(33*D), by adding this line to your
program:
40 PRINT TAN(33*D)
Arc functions
The opposite of the
tangent is called the arctangent:
the tangent of 30° is about .58
the arctangent of .58 is about 30°
Similarly, the opposite of the sine is called the arcsine, and
the opposite of the cosine is called the arccosine.
This program prints the
arctangent of .58, the arcsine of .5, and the arccosine of .87:
10 D=ATN(1)/45
20 PRINT ATN(.58)/D
30 X=.5: PRINT ATN(X/SQR(1-X*X))/D
40 X=.87: PRINT 90-ATN(X/SQR(1-X*X))/D
Line 10 is the special
formula that defines ``D'' to be ``degrees''. Line 20 prints the
arctangent of .58, in degrees. (If you omit the ``/D'', the
computer will print the answer in radians instead of degrees.)
Line 30 sets X equal to .5 and then prints its arcsine (by using
a formula that combines ATN with SQR). Line 40 sets X equal to
.87 and then prints its arccosine (by using a formula that
combines 90 with ATN and SQR). The answer to each of the three
problems is about 30 degrees.
TYPES OF NUMBERS
BASIC can handle three types of numbers: integers, real
numbers, and double-precision numbers.
Generally speaking, an integer is a number that has no decimal
point; a real number has a decimal point but no more that 7
digits; a double-precision number has a decimal point and more
than 7 digits. For example, -27 is an integer, -27.51431 is a
real number, and -27.514318 is a double-precision number.
Even though -27 is an integer, -27.0 is not an integer: it's a
real number instead. -27.000000 is a double-precision number.
The highest permissible integer is 32767; the lowest
permissible integer is -32768. If you try to type an integer
higher than 32767 or lower than -32768, the computer will
automatically add a decimal point at the end of the number, so
that the number becomes real or double-precision. (The decimal
point will appear in the computer's RAM but not necessarily on
your screen.)
Any number that contains an E (such as 7E2) is real, even if
the number contains no decimal point or contains more than 7
digits. To make such a number to be double-precision instead,
type a D instead of an E (like this: 7D2).
Accuracy
The computer handles integers accurately. If you type a real
number, the computer tries to handle it accurately, but sometimes
makes slight mistakes with the 7th digit. If you type a
double-precision number, the computer handles the first 16 digits
accurately, but makes slight mistakes with the 17th digit.
Speed
The computer handles integers quickly, real numbers slowly, and
double-precision numbers very slowly.
RAM consumption
When the computer handles numbers, it automatically compresses
them so that the numbers consume very little RAM. Each integer
consumes just 2 bytes of RAM, each real number consumes 4 bytes,
and each double-precision number consumes 8 bytes.
For example, if your program says DIM X(50,20), the array X
contains 50 rows of 20 numbers, making 1000 numbers altogether;
and if each number is real (4 bytes), the entire array consumes
4000 bytes ___ theoretically. In practice, the array also
contains a few extra bytes, for bureaucratic overhead. So the
array contains slightly more than 4000 bytes ___ which is roughly
4K.
On most computers, BASIC is limited to 64K: if you buy extra
RAM beyond 64K, BASIC ignores it. Most of that 64K is consumed by
the lines of your program, the DOS, and BASIC itself, so only a
few K are left for arrays.
Variables
An ordinary variable
(such as X) stands for a real number. For example, you can say
X=3.7, which makes X be the real number 3.7. If you say X=3, the
computer will make X be 3.0 instead to make X be a real number.
You can create four
kinds of variables. A simple variable (such as X) or a variable
that ends in an exclamation point (such as X!) is a real; a
variable that ends in a dollar sign (such as X$) is a string; a
variable that ends in a percent sign (such as X%) is an integer;
a variable that ends in a number sign (such as X#) is
double-precision.
If you begin your
program by saying ___
1 DEFINT A-Z
all the simple variables (such as X) will be integers instead of
reals.
What to do
Write your program
simply, without worrying about which numbers and variables are
integers, real numbers, and double-precision numbers. But after
your program is finished and debugged, edit the program, by
making the following changes, which improve the program's speed
and accuracy.
All integers If your
program doesn't involve any decimals or huge numbers, make line 1
say DEFINT A-Z. That will turn every variable into an integer, so
that the program runs faster and consumes less RAM.
Mostly integers If your
program involves just a few decimals or huge numbers, make line 1
say DEFINT A-Z, and put an exclamation point after every variable
that stands for a decimal or huge number.
Extra accuracy If you
want to perform one of the computations extra-accurately, put a
number sign after all variables the computation involves (for
example, say X# instead of X), and put at least 8 digits in each
number (for example, say 2.4000000 instead of 2.4).
Before you make those
changes, check the ``Versions of BASIC'' appendix to find out
whether your computer is different.